package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.model.McpCarDealerInformation;
import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.model.McpUserOtherInformation;
import com.pgytesting.secondhandcar.dao.McpUserOtherInformationDao;
import com.pgytesting.secondhandcar.service.McpUserOtherInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户其他信息 (McpUserOtherInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-16 10:11:28
 */
@Slf4j
@Service("mcpUserOtherInformationService")
public class McpUserOtherInformationServiceImpl implements McpUserOtherInformationService {
    @Resource
    private McpUserOtherInformationDao mcpUserOtherInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public McpUserOtherInformation queryById(String id) {
        return this.mcpUserOtherInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<McpUserOtherInformation> queryAllByLimit(int offset, int limit) {
        return this.mcpUserOtherInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param mcpUserOtherInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<McpUserOtherInformation> queryAll(McpUserOtherInformation mcpUserOtherInformation) {
        return this.mcpUserOtherInformationDao.queryAll(mcpUserOtherInformation);
    }

    /**
     * 新增数据
     *
     * @param mcpUserOtherInformation 实例对象
     * @return 实例对象
     */
    @Override
    public McpUserOtherInformation insert(McpUserOtherInformation mcpUserOtherInformation) {
        this.mcpUserOtherInformationDao.insert(mcpUserOtherInformation);
        return mcpUserOtherInformation;
    }

    /**
     * 修改数据
     *
     * @param mcpUserOther 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseMessage update(McpUserOtherInformation mcpUserOther) {
        // 获取到要session中要修改的用户信息
        String userType = String.valueOf(SessionUtil.getSession().getAttribute("login"));
        McpUserInfo mcpUserInfo;
        McpCarDealerInformation carDealerInformation;
        // 判断用户类型，获取唯一标识ID
        if (Parameters.USER_TYPE_CAR.equals(userType)) {
            carDealerInformation = SessionUtil.getSessionMcpCar();
            mcpUserOther.setUserNumber(carDealerInformation.getUserNumber());
        } else if (Parameters.USER_TYPE_USER.equals(userType)) {
            mcpUserInfo = SessionUtil.getSessionMcpUser();
            mcpUserOther.setUserNumber(mcpUserInfo.getUserNumber());
        } else {
            return ResponseMessage.errorResponse("个人信息修改异常");
        }
        int change = 0;
        try {
            change = this.mcpUserOtherInformationDao.update(mcpUserOther);
        }catch (Exception e){
            log.error("修改个人信息异常",e);
            return ResponseMessage.errorResponse("修改个人信息异常");
        }
        if (change < 1){
            return ResponseMessage.errorResponse("修改个人信息失败");
        }
        return ResponseMessage.successResponse("修改成功");
    }
}